﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace IveelySE.NLP.Recognition
{
    /// <summary>
    /// 词性分析后，抽取的有效信息类
    /// </summary>
    [Serializable]
    public class Information
    {

        /// <summary>
        /// 信息编号
        /// </summary>
        public int Id { get; private set; }

        /// <summary>
        /// 参考引用
        /// </summary>
        public string Reference { get; set; }

        /// <summary>
        /// 发生时间
        /// </summary>
        public string HappenTime { get; private set; }

        /// <summary>
        /// 发生地点
        /// </summary>
        public string HappenPlace { get; private set; }

        /// <summary>
        /// 参与任务
        /// </summary>
        public string RelatePeople { get; private set; }

        /// <summary>
        /// 整体描述
        /// </summary>
        public string Describution { get; set; }

        /// <summary>
        /// 初始化信息
        /// </summary>
        /// <param name="infors"></param>
        public void Init(string[] infors)
        {
            this.HappenTime = infors[0];
            this.HappenPlace = infors[1];
            this.RelatePeople = infors[2];
            this.Describution = infors[3];
            this.Id = Math.Abs(infors[3].GetHashCode());
        }

        /// <summary>
        /// 是否是一个合格的信息
        /// </summary>
        /// <returns></returns>
        public bool IsQualified()
        {
            int qualify = 0;
            if (HappenTime.Length > 0 && HappenTime.Length < 20)
            {
                qualify++;
            }
            if (HappenPlace.Length > 0 && HappenPlace.Length < 20)
            {
                qualify++;
            }
            if (RelatePeople.Length > 0 && RelatePeople.Length < 20)
            {
                qualify++;
            }
            return qualify > 2;
        }

        public override string ToString()
        {
            return this.HappenTime + this.HappenPlace + this.RelatePeople + this.RelatePeople + this.Describution + this.HappenTime + this.HappenPlace;
        }

        public string FormatString()
        {
            if (this.Describution.Length > 10)
            {
                string formatInfor =
                    string.Format(
                        "<div class='title'>地点:{1}</div><div class='title'>相关:{2}</div><div class='title'>描述:{3}<div><span class='url'>引用:{4}</span><span class='date'>&nbsp日期:{0}</span>",
                        Filter(this.HappenTime),
                        Filter(this.HappenPlace), Filter(this.RelatePeople),
                        Filter(this.Describution), this.Reference);
                return "<li class='record'>" + formatInfor + "</li>";
            }
            return string.Empty;
        }

        private string Filter(string text)
        {
            IList<string> list = new List<string>();
            string[] values = text.Split(new char[] { ' ' }, StringSplitOptions.RemoveEmptyEntries);
            foreach (string value in values)
            {
                if (!list.Contains(value))
                {
                    list.Add(value);
                }
            }
            return String.Join(" ", list);
        }
    }
}
